<?php

require_once('../../config/maptrick.conf.php');
require_once(SITE_ROOT . "class/db/DBCountry.class.php");

ini_set("memory_limit", "100M");
ini_set("max_execution_time", 600);

try {
	if ($_GET["pass"] != SCRIPT_PWD) die("Incorrect password");	
	
	$dbcountry = new DBCountry();
	$countries = $dbcountry->getCountries("countryID");
	
	$sprite = imagecreatetruecolor(25*149, 15);
	imagealphablending($sprite, false);
	$transparent = imagecolorallocatealpha($sprite, 0, 0, 0, 127);
	imagefill($sprite, 0, 0, $transparent);
	imagesavealpha($sprite, true);
	
	foreach ($countries as $country) {
		if ($country->getCountryID() == 126) {
			$original = SITE_ROOT . "flags/original/" . strtolower("Ivory Coast") . ".png";
		}
		else $original = SITE_ROOT . "flags/original/" . strtolower($country->getEnglishName()) . ".png";
		if (file_exists($original)) {
			if ($country->getCountryID() == 126) {
				$originalflag = imagecreatefrompng(SITE_ROOT . "flags/original/" . strtolower("Ivory Coast") . ".png");
			}
			else $originalflag = imagecreatefrompng(SITE_ROOT . "flags/original/" . strtolower($country->getEnglishName()) . ".png");
			
			$flag = imagecreatetruecolor(25, 15);
			imagealphablending($flag, false);
			$transparent = imagecolorallocatealpha($flag, 0, 0, 0, 127);
			imagefill($flag, 0, 0, $transparent);
			imagesavealpha($flag, true);
			
			imagecopyresampled($flag, $originalflag, 0, 0, 0, 0, 25, 15, 100, 60);
						
			imagecopymerge($sprite, $flag, 25*$country->getCountryID(), 0, 0, 0, 25, 15, 100);
			
			imagedestroy($originalflag);
			imagedestroy($flag);
		}
		else throw new Exception("Original flag for " . $country->getEnglishName() . " doesn't exist<br />");
	}
	imagepng($sprite, SITE_ROOT . "flags/flagsprite.png");
	
	imagedestroy($sprite);
	exit();
}
catch (Exception $e) {
	echo "Generating the flagsprite failed";
	exit();
}

?>

